﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Adapter.SISCONVE.Contract;
using Business.Common.Factory;
using Business.Common.Service.Request;
using Business.Common.Service.Response;
using Business.Contract;
using Business.Contract.ServiceContract;
using DataAccess.UnitTransaction.Contract;

namespace Business.Core
{
    public class FacadeInternalService : IFacadeInternalService
    {
        private static FacadeInternalService _instance = null;
        private IAdapter _adapterSISCONVE;
        
        private FacadeInternalService()
        {
           _adapterSISCONVE = new Adapter.SISCONVE.Core.Adapter();
        }

        public static FacadeInternalService GetInstance
        {
            get
            {
                if (_instance == null)
                {
                    _instance = new FacadeInternalService();
                }
                return _instance;
            }
        }

        #region Methods
        
        public Response CacheStatusDetailVehicles(Request request)
        {
            Response response = new Response();
            try
            {
                using (IUnitTransaction uow = UnitTransactionFactory.CreateUnitOfWork(request.ServiceContext))
                {
                    uow.DAOStatusDetailVehicles.DeleteAllObjects();
                    var vehiclesDetail = _adapterSISCONVE.GetStatusDetailVehicles();
                    foreach (var item in vehiclesDetail)
                    {
                        uow.DAOStatusDetailVehicles.insert(item);
                    }
                    uow.Save();
                }
                response.SetOKResponse();
            }
            catch (Exception ex)
            {
                response.HandleException(ex);
            }
            return response;
        }

        public Response CachePerformanceReportVehicles(Request request)
        {
            Response response = new Response();
            try
            {
                using (IUnitTransaction uow = UnitTransactionFactory.CreateUnitOfWork(request.ServiceContext))
                {
                    uow.DAOPerformanceReportVehicles.DeleteAllObjects();
                    var performanceReportVehicles = _adapterSISCONVE.GetPerformanceReportVehicles();
                    foreach (var item in performanceReportVehicles)
                    {
                        uow.DAOPerformanceReportVehicles.insert(item);
                    }
                    uow.Save();
                }
                response.SetOKResponse();
            }
            catch (Exception ex)
            {
                response.HandleException(ex);
            }
            return response;
        }

        public Response CacheReportVehicles(Request request)
        {
            Response response = new Response();
            try
            {
                using (IUnitTransaction uow = UnitTransactionFactory.CreateUnitOfWork(request.ServiceContext))
                {
                    uow.DAOReportVehicles.DeleteAllObjects();
                    var reportVehicles = _adapterSISCONVE.GetReportVehicles();
                    foreach (var item in reportVehicles)
                    {
                        uow.DAOReportVehicles.insert(item);
                    }
                    uow.Save();
                }
                response.SetOKResponse();
            }
            catch (Exception ex)
            {
                response.HandleException(ex);
            }
            return response;
        }

        #endregion
    }
}
